<?php
/**
* 数据库单列模式
*/
class DB
{
	private static $_instance;
	private static $_connectSource;
	private static $_dbConfig = array(
		'host' => 'localhost',
		'username' => 'root',
		'password' => '',
		'database' => 'app',
		'charset' => 'utf8',
	);
	private function __construct()
	{
	}

	public static function getInstance()
	{
		if (!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	public function connect()
	{
		if (!self::$_connectSource) {
			self::$_connectSource = @new mysqli(self::$_dbConfig['host'], self::$_dbConfig['username'], self::$_dbConfig['password'], self::$_dbConfig['database']);
			if (self::$_connectSource->connect_error) {
				throw new Exception('mysql connect error:' . self::$_connectSource->connect_error);
				exit();
			}
			self::$_connectSource->set_charset(self::$_dbConfig['charset']);
		}
		return self::$_connectSource;
	}
}